package springmvc.param.controller;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * description: 05
 * TODO：获取cookie
 *
 * 1. save方法保存 cookie, http://localhost:8080/cookie/save
 * 2. cookie 是kv格式，通过 HttpServletResponse 添加后返回给客户端
 * 3. data方法获取 cookie, http://localhost:8080/cookie/data
 * 4. @CookieValue 修饰形参，指定获取的cookie的key
 */

@Controller
@RequestMapping("cookie")
@ResponseBody
public class CookieController {

    @RequestMapping("data")
    public String data(@CookieValue(value = "cookieName") String value){
        System.out.println("value = " + value);
        return value;
    }

    @GetMapping("save")
    public String save(HttpServletResponse response){
        Cookie cookie = new Cookie("cookieName","root");
        response.addCookie(cookie);
        return "ok";
    }
}
